package de.bht.fb6.cg1.exercise1;

import java.awt.Canvas;
import java.awt.Color;
import java.awt.GridLayout;
import java.util.HashSet;
import java.util.Set;

import javax.swing.JFrame;

/**
 * This class represents the ColorConverter.
 * <p/>
 * An application to convert colors between differen color models.
 * 
 * @author Stephan Rehfeld <rehfeld (-at-) beuth-hochschule.de>
 * @author Dominik Eckelmann
 */
@SuppressWarnings("serial")
public class ColorConverterJFrame extends JFrame implements ColorSyncListener {

    /**
     * The main method.
     * 
     * @param args Console parameters. This main method does not consume any parameters.
     */
    public static void main(final String args[]) {

        java.awt.EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                new ColorConverterJFrame().setVisible(true);
            }
        });
    }

    /**
     * This is the canvas that shows the current color.
     */
    private Canvas colorCanvas;

    /**
     * The panel that holds all controls for RGB color.
     */
    private Set<ColorPanel> panels;

    /**
     * This constructor creates a new Frame of the ColorConverter with the size 640x480. It already close it self if
     * requested and has the correct frame title.
     */
    public ColorConverterJFrame() {
        super("Color Converter");

        setSize(640, 480);
        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        setLayout(new GridLayout(0, 1));

        createColorCanvas();
        createPanels();

        pack();

    }

    /**
     * This method creates the color canvas
     */
    private void createColorCanvas() {
        colorCanvas = new Canvas();
        colorCanvas.setBackground(Color.BLACK);
        add(colorCanvas);

    }

    /**
     * This method creates the panels for every color space
     */
    private void createPanels() {
        panels = new HashSet<ColorPanel>();

        final ColorModelInterface[] models = new ColorModelInterface[] {
                new RGB(Color.BLACK), new YUV(Color.BLACK), new CMY(Color.BLACK)
        };

        for (final ColorModelInterface model : models) {
            final ColorPanel newPan = new ColorPanel(model);
            add(newPan);
            newPan.addListener(this);
            panels.add(newPan);
        }

        int i = 0;
        for (final ColorPanel reciever : panels) {
            int j = 0;
            for (final ColorPanel listener : panels) {
                if (i == j) {
                    j++;
                    continue;
                }
                reciever.addListener(listener);
                j++;
            }
            i++;
        }
    }

    /**
     * catches color changes and applies the new to the {@link ColorConverterJFrame#colorCanvas}
     * @param event     informations about the new color
     */
    @Override
    public void colorChanged(final ColorSyncEvent event) {
        colorCanvas.setBackground(event.getColor());
    }
}
